package com.android.pantryshopping;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.pantryshopping.bean.Commodity;
import com.android.pantryshopping.util.BitMapTool;

public class AddActivity extends AppCompatActivity {

    private CommoditySqLiteHelper mCommoditySqLiteHelper;

    private EditText mEdtId;
    private ImageView mImgAdd;
    private EditText mEdtName;
    private EditText mEdtPrice;
    private EditText mEdtNum;
    private Button mBtnCancel;
    private Button mBtnDetermine;
    private String mPicturePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mBtnDetermine.setOnClickListener(v -> {
            Commodity model = new Commodity();
            model.setId(mEdtId.getText().toString().trim());
            model.setName(mEdtName.getText().toString().trim());
            model.setPrice(mEdtPrice.getText().toString().trim());
            model.setQuantity(mEdtNum.getText().toString().trim());
            model.setImage(mPicturePath);

            boolean isSuccess = mCommoditySqLiteHelper.addCommodityData(model);
            Toast.makeText(this,isSuccess ? "Add Success" : "Add Fail",Toast.LENGTH_SHORT).show();
            finish();
        });
        mBtnCancel.setOnClickListener(v -> {
            finish();
        });
        mImgAdd.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent,200);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && resultCode == RESULT_OK && data != null) {
            Uri selectImage = data.getData();
            String [] filePath = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectImage,filePath,null,null,null);
            cursor.moveToFirst();

            int index = cursor.getColumnIndex(filePath[0]);
            mPicturePath = cursor.getString(index);
            cursor.close();

            Bitmap bitmap = BitmapFactory.decodeFile(mPicturePath);

            mImgAdd.setImageBitmap(bitmap);
        }
    }

    private void initData() {
        mCommoditySqLiteHelper = new CommoditySqLiteHelper(this,"Commodity_db",null,1);
    }

    private void initView() {
        mEdtId = findViewById(R.id.edt_id);
        mImgAdd = findViewById(R.id.img_add);
        mEdtName = findViewById(R.id.edt_name);
        mEdtPrice = findViewById(R.id.edt_price);
        mEdtNum = findViewById(R.id.edt_num);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mBtnDetermine = findViewById(R.id.btn_determine);
    }
}